<template>
  <div class="wholesaler-reconciliation">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">📦 批发商对账</span>
          <div class="header-actions">
            <el-button type="primary" @click="handleGenerate">
              <el-icon><DocumentAdd /></el-icon>
              生成对账单
            </el-button>
            <el-button type="success" @click="handleBatchSettle">
              <el-icon><Money /></el-icon>
              批量结算
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="批发商">
          <el-input v-model="searchForm.wholesalerName" placeholder="请输入批发商名称" clearable />
        </el-form-item>
        <el-form-item label="对账周期">
          <el-date-picker
            v-model="searchForm.period"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="结算状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="未结算" value="unsettled" />
            <el-option label="部分结算" value="partial" />
            <el-option label="已结算" value="settled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 统计卡片 -->
      <el-row :gutter="20" class="stat-cards">
        <el-col :span="6">
          <div class="stat-card receivable">
            <div class="stat-label">应收总额</div>
            <div class="stat-value">¥{{ totalReceivable.toLocaleString() }}</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card received">
            <div class="stat-label">已收金额</div>
            <div class="stat-value">¥{{ totalReceived.toLocaleString() }}</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card unreceived">
            <div class="stat-label">未收金额</div>
            <div class="stat-value">¥{{ totalUnreceived.toLocaleString() }}</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-label">批发商数</div>
            <div class="stat-value">{{ wholesalerCount }}</div>
          </div>
        </el-col>
      </el-row>

      <!-- 对账列表 -->
      <el-table :data="tableData" border stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="对账单号" width="150" />
        <el-table-column prop="wholesalerName" label="批发商名称" width="200" />
        <el-table-column prop="period" label="对账周期" width="180">
          <template #default="{ row }">
            {{ row.startDate }} 至 {{ row.endDate }}
          </template>
        </el-table-column>
        <el-table-column prop="orderCount" label="订单数" width="100" />
        <el-table-column prop="totalAmount" label="销售金额" width="130">
          <template #default="{ row }">
            <span class="amount-text">¥{{ row.totalAmount.toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="receivedAmount" label="已收金额" width="120">
          <template #default="{ row }">
            <span style="color: #67c23a; font-weight: bold;">¥{{ row.receivedAmount.toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="unreceived" label="未收金额" width="120">
          <template #default="{ row }">
            <span style="color: #f56c6c; font-weight: bold;">¥{{ (row.totalAmount - row.receivedAmount).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTag(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleViewDetail(row)">查看明细</el-button>
            <el-button 
              v-if="row.totalAmount > row.receivedAmount" 
              link 
              type="success" 
              @click="handleReceive(row)"
            >
              收款登记
            </el-button>
            <el-button link type="warning" @click="handleRemind(row)">催款</el-button>
            <el-button link type="info" @click="handleExport(row)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
      />
    </el-card>

    <!-- 收款登记对话框 -->
    <el-dialog 
      v-model="receiveDialogVisible" 
      title="收款登记"
      width="600px"
    >
      <el-form :model="receiveForm" label-width="100px">
        <el-form-item label="对账单号">
          <el-input v-model="receiveForm.reconciliationId" disabled />
        </el-form-item>
        <el-form-item label="批发商">
          <el-input v-model="receiveForm.wholesalerName" disabled />
        </el-form-item>
        <el-form-item label="应收金额">
          <span class="amount-highlight">¥{{ receiveForm.totalAmount?.toLocaleString() }}</span>
        </el-form-item>
        <el-form-item label="已收金额">
          <span style="color: #67c23a;">¥{{ receiveForm.receivedAmount?.toLocaleString() }}</span>
        </el-form-item>
        <el-form-item label="未收金额">
          <span style="color: #f56c6c;">¥{{ receiveForm.unreceived?.toLocaleString() }}</span>
        </el-form-item>
        <el-divider />
        <el-form-item label="本次收款">
          <el-input-number 
            v-model="receiveForm.receiveAmount" 
            :min="0" 
            :max="receiveForm.unreceived"
            :precision="2"
          />
        </el-form-item>
        <el-form-item label="收款方式">
          <el-select v-model="receiveForm.paymentMethod">
            <el-option label="银行转账" value="bank" />
            <el-option label="支票" value="check" />
            <el-option label="现金" value="cash" />
            <el-option label="承兑汇票" value="acceptance" />
          </el-select>
        </el-form-item>
        <el-form-item label="收款日期">
          <el-date-picker
            v-model="receiveForm.receiveDate"
            type="date"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input 
            v-model="receiveForm.remark" 
            type="textarea" 
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="receiveDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleReceiveSubmit">确认收款</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { DocumentAdd, Money } from '@element-plus/icons-vue'

const searchForm = reactive({
  wholesalerName: '',
  period: null,
  status: ''
})

const selectedRows = ref([])

const totalReceivable = ref(3456789)
const totalReceived = ref(2856234)
const totalUnreceived = ref(600555)
const wholesalerCount = ref(35)

const tableData = ref([
  {
    id: 'DZ-WHO-202401001',
    wholesalerName: '华南批发商',
    startDate: '2024-01-01',
    endDate: '2024-01-31',
    orderCount: 145,
    totalAmount: 856234,
    receivedAmount: 856234,
    status: 'settled'
  },
  {
    id: 'DZ-WHO-202401002',
    wholesalerName: '华北批发商',
    startDate: '2024-01-01',
    endDate: '2024-01-31',
    orderCount: 98,
    totalAmount: 656789,
    receivedAmount: 400000,
    status: 'partial'
  },
  {
    id: 'DZ-WHO-202401003',
    wholesalerName: '华东批发商',
    startDate: '2024-01-01',
    endDate: '2024-01-31',
    orderCount: 234,
    totalAmount: 1234567,
    receivedAmount: 0,
    status: 'unsettled'
  }
])

const pagination = reactive({
  page: 1,
  size: 10,
  total: 3
})

const receiveDialogVisible = ref(false)
const receiveForm = reactive({
  reconciliationId: '',
  wholesalerName: '',
  totalAmount: 0,
  receivedAmount: 0,
  unreceived: 0,
  receiveAmount: 0,
  paymentMethod: '',
  receiveDate: '',
  remark: ''
})

const getStatusTag = (status) => {
  const map = { unsettled: 'danger', partial: 'warning', settled: 'success' }
  return map[status] || 'info'
}

const getStatusText = (status) => {
  const map = { unsettled: '未结算', partial: '部分结算', settled: '已结算' }
  return map[status] || status
}

const handleSearch = () => {
  ElMessage.success('查询成功')
}

const handleReset = () => {
  Object.assign(searchForm, { wholesalerName: '', period: null, status: '' })
  handleSearch()
}

const handleSelectionChange = (selection) => {
  selectedRows.value = selection
}

const handleGenerate = () => {
  ElMessage.info('生成对账单')
}

const handleReceive = (row) => {
  Object.assign(receiveForm, {
    reconciliationId: row.id,
    wholesalerName: row.wholesalerName,
    totalAmount: row.totalAmount,
    receivedAmount: row.receivedAmount,
    unreceived: row.totalAmount - row.receivedAmount,
    receiveAmount: row.totalAmount - row.receivedAmount
  })
  receiveDialogVisible.value = true
}

const handleReceiveSubmit = () => {
  ElMessage.success('收款登记成功')
  receiveDialogVisible.value = false
  handleSearch()
}

const handleViewDetail = () => {
  ElMessage.info('查看明细')
}

const handleRemind = (row) => {
  ElMessage.success(`已向 ${row.wholesalerName} 发送催款通知`)
}

const handleExport = (row) => {
  ElMessage.success(`正在导出对账单 ${row.id}`)
}

const handleBatchSettle = () => {
  ElMessage.info('批量结算')
}
</script>

<style scoped>
.wholesaler-reconciliation {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-form {
  margin-bottom: 20px;
}

.stat-cards {
  margin-bottom: 20px;
}

.stat-card {
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  text-align: center;
}

.stat-card.receivable {
  border-left: 4px solid #409eff;
}

.stat-card.received {
  border-left: 4px solid #67c23a;
}

.stat-card.unreceived {
  border-left: 4px solid #f56c6c;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 10px;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
}

.amount-text {
  color: #409eff;
  font-weight: bold;
}

.amount-highlight {
  color: #f56c6c;
  font-weight: bold;
  font-size: 18px;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>

